//
//  AirThread.h
//  AirCpp
//
//  Created by Penghao on 14-1-17.
//  Copyright (c) 2014年 PengHao.

//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#ifndef __AirCpp__AirThread__
#define __AirCpp__AirThread__

#define OSLinux

#include <iostream>
#include "../AirObject/AirObject.h"
#include "../AirContainer/AirArray.h"
#include "AirOperation.h"
#include "../AirOSInclude/AirOSInclude.h"

namespace AirCpp{
    class AirThreadLock : public AirObject{
    protected:
        typedef AirLinuxThreadLock AirOSThreadLock;
        AirOSThreadLock thread_lock;
    public:
        int init(){
            return thread_lock.init();
        }
        
        int lock(){
            return thread_lock.lock();
        }
        int unlock(){
            return thread_lock.unlock();
        }
        
        int try_lock(){
            return thread_lock.try_lock();
        }
        
        int try_destroy(){
            return thread_lock.try_destroy();
        }
    };
    
    class AirSemaphore : public AirObject{
    protected:
        typedef AirLinuxSemaphore AirOSSemaphore;
        AirOSSemaphore semaphore;
    public:
        int init(bool shared){
            return semaphore.init(shared);
        }
        
        int wait(){
            return semaphore.wait();
        }
        
        int post(){
            return semaphore.post();
        }
        
        int destroy(){
            return semaphore.destroy();
        }
    };
    
    class AirThreadRWLock : public AirObject{
    protected:
    };
    
    template <class T>
    class AirThread : public AirObject{
    protected:
        typedef AirLinuxThread<T> AirOSThread;
        AirOSThread *p_thread;
        AirOperation<T> *oper;
    public:
        AirThread(){
            p_thread = NULL;
            oper = NULL;
        }
        
/**
  *	初始化线程
  *
  *	@return	初始化失败返回-1 成功返回0
  */
         int init(){
            p_thread = new AirOSThread();
            int rs = p_thread->init();
            if (rs < 0) {
                release(p_thread);
                p_thread = NULL;
            }
            return rs;
        }
        
/**
  *	运行操作
  *
  *	@param 	_oper 	操作对象
  *
  *	@return	成功返回0 失败返回错误码
  */
         int run_operation(AirOperation<T> *_oper){
            oper = retain(_oper);
            return p_thread->run_operation(oper);
         };
        
        int run_operation(T *t, void (T::*fun)(void *), void *argments){
            oper = new AirOperation<T>();
            oper->init(t, fun, argments);
            return p_thread->run_operation(oper);
        }
        
/**
  *	终止运行
  *
  */
        void cancel(){
            p_thread->cancel();
        }
/**
  *	阻塞等待运行结束
  *
  *	@param 	*ret    运行返回结果
  */
        void wait_until_done(void **ret){
            p_thread->wait_until_done(ret);
        }
        
        void keep_going(){
            p_thread->keep_going();
        }
        
        ~AirThread(){
            release(oper);
        }
    };
}

#endif /* defined(__AirCpp__AirThread__) */
